package com.hawk.eye.server.webhook;

import com.hawk.eye.api.webhook.IWebhookRequester;
import com.hawk.eye.client.toolkit.HawkEyeLogger;
import com.hawk.eye.common.model.ServiceAlarmMessage;
import com.hawk.eye.common.utils.http.OkHttpUtil;
import com.hawk.eye.framework.utils.ExceptionUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @className: HttpWebhookRequester
 * @description:
 * @author: salad
 * @date: 2023/10/4
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class HttpWebhookRequester implements IWebhookRequester {


    @Override
    public String identity() {
        return "HTTP(POST请求)";
    }

    @Override
    public void send(ServiceAlarmMessage sam) {
        try {
            String resultAsString = OkHttpUtil.postJson(sam.getWebhookConfigMessage().getWebhook(), sam.getContent());
            log.info("[{}]响应结果:{}", identity(), resultAsString);
        } catch (Throwable e) {
            HawkEyeLogger.errorWithSkipSend(getClass(), identity() + "请求失败:{}", ExceptionUtil.getExceptionMessage(e));
        }
    }
}
